package com.hfmall.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hfmall.entity.HfCategory;
import com.hfmall.entity.HfGoods;
import com.hfmall.entity.HfGoodsinfo;
import com.hfmall.entity.ResponseCode;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hfmall.entity.*;
import com.hfmall.service.impl.HfCategoryServiceImpl;
import com.hfmall.service.impl.HfGoodsServiceImpl;
import com.hfmall.service.impl.HfGoodsinfoServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liclo
 * @since 2021-08-12
 */
@Slf4j
@CrossOrigin()
@RestController
@RequestMapping("goodsinfo")
public class HfGoodsinfoController {
    public List<HfGoodsinfo> hfGoodsinfoList;
    @Autowired
    private HfGoodsinfoServiceImpl hfGoodsinfoService;
    @Autowired
    private HfGoodsServiceImpl hfGoodsService;
    @Autowired
    private HfCategoryServiceImpl hfCategoryService;

    @GetMapping("list")
    public ResponseCode list(@Param("current") Integer current, @Param("size") Integer size) {
        return new ResponseCode(hfGoodsinfoService.getGoodsInfo(current, size));
    }

    @PostMapping("getList")
    public ResponseCode getList(@RequestBody JSONObject json) {
        JSONObject data = JSON.parseObject(json.toJSONString());
        Integer current = Integer.valueOf(data.getString("current"));
        Integer size = Integer.valueOf(data.getString("size"));
        HfGoodsinfo goodsinfo = JSON.parseObject(data.getString("searchList"), HfGoodsinfo.class);

        return new ResponseCode(hfGoodsinfoService.getByCondition(current, size, goodsinfo));
    }

    @GetMapping("hot")
    public ResponseCode isHot(int num) {
        return new ResponseCode(hfGoodsinfoService.getHot(num));
    }

    @GetMapping("byCategory")
    public ResponseCode getListByCategory(int page, int num, int pid) {
        return new ResponseCode(hfGoodsinfoService.getListByCategory(page, num, pid));
    }

    @RequestMapping("FLlist") //根据分类id(info_pid)查询 商品共性表->再查对应商品详情(含图片查询)
    public ResponseCode getGoodsinfoList(int id) { //所选分类的id
        List<HfCategory> hfCategoryLv3 = hfCategoryService.getCategoryLv3Byid(id);;//对应分类的所有三级分类
//        log.debug("....>>>>>>{}",hfCategoryLv3);
        List<HfGoodsinfo> hfGoodsinfoListAll=hfGoodsinfoService.getAllGoodsinfoByByCategoryL3Id(hfCategoryLv3);
        return new ResponseCode(hfGoodsinfoListAll);
    }



     @RequestMapping("allList") //分页查询所有goodsinfo
    public ResponseCode getAllGoodsinfo(Integer current) {
         Page<HfGoodsinfo> page=hfGoodsinfoService.getGoodsInfo(current,null);
        return new ResponseCode(page);
    }


    @RequestMapping("likeList") //分页查询所有goodsinfo
    public ResponseCode getAllGoodsinfoByLike( String msg,Integer current) {
        Integer page=0;
        if(current!=null){
            page=current;
        }
        Integer size=10;
//        log.debug("-----:{}",hfGoodsinfoService.getGoodsinfoListByLike(msg,page,size));
        return new ResponseCode(hfGoodsinfoService.getGoodsinfoListByLike(msg,page,size));
    }

    @RequestMapping("pageGoodsinfoListByPid") //分页查询指定类别goodsinfo
    public ResponseCode getPageGoodsinfoListByCategoryId(int id){
//        log.debug("{}",id);
        return new ResponseCode (hfGoodsinfoService.getPageGoodsinfoListByCategoryId(id));
    }
    @RequestMapping("pageGoodsinfoListByCondition") //分页查询指定类别goodsinfo
    public ResponseCode getHfGoodsinfoListByCategoryL3Id(UtilPageGoodsinfoVO condition){
        if(condition.getClassValue()==null){
            return new ResponseCode(333,"all");
        }else {
            return new ResponseCode(hfGoodsinfoService.getHfGoodsinfoListByCategoryL3Id(condition));
        }
    }


    @GetMapping("upsta")
    public ResponseCode upsta(String status, Integer id) {
        UpdateWrapper<HfGoodsinfo> wrapper = new UpdateWrapper();
        wrapper.set("status", status).eq("id", id);
        hfGoodsinfoService.update(wrapper);
        return ResponseCode.SUCCESS;
    }

    @GetMapping("uphot")
    public ResponseCode uphot(String hot, Integer id) {
        UpdateWrapper<HfGoodsinfo> wrapper = new UpdateWrapper();
        wrapper.set("hot", hot).eq("id", id);
        hfGoodsinfoService.update(wrapper);
        return ResponseCode.SUCCESS;
    }

    @PostMapping("add")
    public ResponseCode<Void> add(HttpServletRequest request) throws IOException {
        hfGoodsinfoService.add(request);
        return ResponseCode.SUCCESS;
    }

}
